Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background zonal flow U or U(y) in the SingleLayerQG module #360

Merged
merged 39 commits into from
Jul 29, 2024

Conversation

mncrowe
Copy link
Contributor

@mncrowe mncrowe commented Jun 17, 2024

[edited] Add a constant background zonal flow in the $U$ which can also be varying in $y$, i.e., $U(y)$.
When the background advection is constant (not varying with $y$) it's included in the linear term L of the equation; when it's varying with $y$ it's included in the nonlinear term N alongside with contributions from background PV gradient $\partial_y^2 U$.

@mncrowe
Copy link
Contributor Author

mncrowe commented Jul 22, 2024

I've added a draft of the full functionality with U = U(y) (periodic in y). One question: when evaluating CalcN! there's an assignment Uy = real.(ifft(im * grid.l .* fft(params.U))). This seems to be required as there's no 1D fft plan in the grid that could be used with mul!. My questions:

  1. Is this the best way to do this derivative?
  2. An identical value is calculated and assigned each timestep. As it's only in the y direction it shouldn't take too much memory, however, is this step (in particular, assigning GPU memory) likely to slow down the timestepping?
  3. We could have Uy saved in params but this seems unnecessary. Do you agree?

src/singlelayerqg.jl Outdated Show resolved Hide resolved
src/singlelayerqg.jl Outdated Show resolved Hide resolved
@navidcy
Copy link
Member

navidcy commented Jul 27, 2024

@mncrowe is it OK if I push few edits?

@navidcy navidcy self-requested a review July 27, 2024 05:02
Copy link
Member

@navidcy navidcy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mncrowe this looks very good! Thanks!

I changed the tests for when U is not varying with y to utilize the Rossby wave test.

There are two things left to do:

  1. remove the if params.U isa Number ... else ... statement from calc_advection!
    https://github.com/mncrowe/GeophysicalFlows.jl/blob/dd3c1ad4b6b3a3b2866525ad3ae993f3f3329270/src/singlelayerqg.jl#L352-L368

which reduces performance; and

  1. add a test for a background flow U(y). This can be done via the test_1layerqg_nonlinearadvection test,

https://github.com/mncrowe/GeophysicalFlows.jl/blob/dd3c1ad4b6b3a3b2866525ad3ae993f3f3329270/test/test_singlelayerqg.jl#L257

similarly to how this was done for the MultilayerQG module; see

https://github.com/mncrowe/GeophysicalFlows.jl/blob/dd3c1ad4b6b3a3b2866525ad3ae993f3f3329270/test/test_multilayerqg.jl#L215-L305

@navidcy
Copy link
Member

navidcy commented Jul 28, 2024

I think I see a minor problem. When U is just a number, don't we need a term - U ∂η/∂x? At the moment this term is not included.

I think I'll try to refactor the code to mimic the MultiLayerQG where the background PV gradients include the ∂η/∂x and ∂η/∂y terms and the ∂²U/∂y² (if U is not a constant).

@navidcy
Copy link
Member

navidcy commented Jul 28, 2024

@mncrowe I think I did everything! Have a look and let me know if you find any issues running the code or if the code is not behaving as you expect? Otherwise I'm happy to merge this!

@navidcy
Copy link
Member

navidcy commented Jul 28, 2024

I would just appreciate your look on the code. In principle I can approve the PR but it'd be nice if somebody else has a look because I made few changes.

@navidcy navidcy changed the title Add constant background flow into SingleLayerQG Add background zonal flow U or U(y) in the SingleLayerQG module Jul 28, 2024
@navidcy navidcy added the 🤥 enhancement New feature or request label Jul 28, 2024
@navidcy navidcy self-requested a review July 28, 2024 11:42
@mncrowe
Copy link
Contributor Author

mncrowe commented Jul 29, 2024

Thanks for the very thorough reviewing and rewrite. Is there anything outstanding for me to do?

@navidcy
Copy link
Member

navidcy commented Jul 29, 2024

Does it work as expected?

@navidcy
Copy link
Member

navidcy commented Jul 29, 2024

A flow at rest advected by a background imposed U=0.5 over some topography.

using GeophysicalFlows, GLMakie, Printf

    dev = CPU()
      n = 256
stepper = "FilteredRK4"
     dt = 0.02
 nsteps = 4000
 nsubs  = 5
      L = 2π

σx, σy = 0.4, 0.8
topographicPV(x, y) = 3exp(-(x - 1)^2 / 2σx^2 - (y - 1)^2 / 2σy^2) - 2exp(- (x + 1)^2 / 2σx^2 - (y + 1)^2 / 2σy^2)

prob = SingleLayerQG.Problem(dev; nx=n, Lx=L, eta=topographicPV, U=0.5, dt, stepper, aliased_fraction=0)
sol, clock, vars, params, grid = prob.sol, prob.clock, prob.vars, prob.params, prob.grid

x,  y  = grid.x,  grid.y
Lx, Ly = grid.Lx, grid.Ly

q = Observable(Array(vars.q))

fig = Figure(size=(380, 300))

axis_kwargs = (xlabel = "x", ylabel = "y", aspect = 1,
               limits = ((-Lx/2, Lx/2), (-Ly/2, Ly/2)))

title_q = Observable("initial vorticity ∂v/∂x-∂u/∂y")
axq = Axis(fig[1, 1]; title = title_q, axis_kwargs...)

hm = heatmap!(axq, x, y, q, colormap = :balance, colorrange = (-5, 5))

Colorbar(fig[1, 2], hm)

contour!(axq, x, y, params.eta;
         levels = collect(0.5:1:2.5), linewidth = 2, color = (:black, 0.5))
contour!(axq, x, y, params.eta;
         levels = collect(-2.5:1:-0.5), linewidth = 2, color = (:grey, 0.7), linestyle = :dash)

title_q[] = "vorticity, t=" * @sprintf("%.2f", clock.t)

record(fig, "singlelayerqg_topography_U0.mp4", 0:round(Int, nsteps/nsubs), framerate = 40) do j
  q[] = irfft(sol, grid.nx)
  title_q[] = "vorticity, t=" * @sprintf("%.2f", clock.t)
  stepforward!(prob, nsubs)
end
singlelayerqg_topography_U0.mp4

@mncrowe
Copy link
Contributor Author

mncrowe commented Jul 29, 2024

Does it work as expected?

All my scripts are producing the same results as before, and match my earlier results using Dedalus.

@navidcy
Copy link
Member

navidcy commented Jul 29, 2024

Nice! I’m merging then.

@navidcy navidcy merged commit 14f581b into FourierFlows:main Jul 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤥 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add background flow functionality in SingleLayerQG model
3 participants